<?php

namespace Common\Logic;

class BaseLogic {

    static $res;

    public function __construct() {
        static::$res = A('Common/Res', 'Logic');
    }

    /**
     * 对表单数据进行验证
     * @param type $fields
     * @param type $orgData
     * @return type
     */
    public function verifyData($fields, $orgData) {
        if (empty($fields)) {
            return static::$res->err("验证规则不能为空");
        }

        $data = array();
        foreach ($fields as $title => $vo) {
            $val = $orgData[$title];
            if (!empty($vo['empty'])) {
                if (empty($val) && $val !== "0") {
                    return static::$res->err($vo['empty']);
                }
            }

            //手机号验证
            if (!empty($vo['mobile'])) {
                if (!validate($val, "mobile")) {
                    return static::$res->err($vo['mobile']);
                }
            }
            
            //身份证验证
            if (!empty($vo['cid'])) {
                if (!validate($val, "card_no")) {
                    return static::$res->err($vo['cid']);
                }
            }

            //性别验证
            if (!empty($vo['sex'])) {
                if ($val != 1 && $val != 2 && $val != 3) {
                    return static::$res->err($vo['sex']);
                }
            }

            //日期格式
            if (!empty($vo['isDate'])) {
                $val = date('Y-m-d', strtotime($val));
            }
            
            //转为整数
            if (!empty($vo['isInt'])) {
                $val = (int)$val;
            }

            $data[$title] = $val;
        }
        return static::$res->suc("成功", $data);
    }

}
